package com.liuxinlong.jobs;

import com.liuxinlong.api.WeChatMessageInterface;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.MessageContentTypeEnum;
import com.liuxinlong.enums.MessagePushBusinessEnum;
import com.liuxinlong.modules.dao.ProjectPatentExpandDao;
import com.liuxinlong.modules.dao.WorkFlowDao;
import com.liuxinlong.modules.entity.MessageConfiguration;
import com.liuxinlong.modules.entity.MessagePushRecord;
import com.liuxinlong.modules.entity.WorkFlow;
import com.liuxinlong.modules.patent.dto.PatentProjectDto;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险专利监控专利排查提醒定时任务
 */
@Service
@Slf4j
@Component("checkPatentProjectTask")
public class CheckPatentProjectTask {

    @Autowired
    private WeChatMessageInterface weChatMessageInterface;

    @Autowired
    private ProjectPatentExpandDao projectPatentExpandDao;

    @Autowired
    private WorkFlowDao workFlowDao;

    /**
     * 业务逻辑
     * 项目进入系统后一月未发起风险排查需求，每周早上9:00通知项目经理登录处理
     */
    public void reptilian() {
        log.info("checkPatentProjectTask start...");
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        String currentDate = currentTime.substring(0, 10);
        List<PatentProjectDto> patentExpandList = projectPatentExpandDao.getPendingProjectList(currentDate, 1);

        Map<String, List<WorkFlow>> workFlowMap = new HashMap<>();
        List<PatentProjectDto> needPushList = new ArrayList<>();
        if (ObjectUtils.isEmpty(patentExpandList)) {
            log.info("no project need to push.");
            log.info("checkPatentProjectTask end...");
            return;
        } else {
            List<String> businessIdList = new ArrayList<>();
            for (PatentProjectDto dto : patentExpandList) {
                float disDays = TimeUtils.calculateTimeGapDay(dto.getCreateTime(), currentTime, TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
                BigDecimal decimal = new BigDecimal(disDays);
                int days = decimal.intValue();
                int quotient = days / 7;
                int remainder = days % 7;
                if (quotient > 3 && remainder == 2) {
                    businessIdList.add(dto.getId());
                    needPushList.add(dto);
                }
            }
            if (businessIdList.size() == 0) {
                log.info("no project need to push.");
                log.info("checkPatentProjectTask end...");
                return;
            }
            List<WorkFlow> workFlowList = workFlowDao.getWorkflowListByIds(businessIdList);
            if (!ObjectUtils.isEmpty(workFlowList)) {
                for (WorkFlow workFlow : workFlowList) {
                    String businessId = workFlow.getBusinessId();
                    List<WorkFlow> workFlows = workFlowMap.containsKey(businessId) ? workFlowMap.get(businessId) : new ArrayList<>();
                    workFlowMap.put(businessId, workFlows);
                }
            }
        }
        for (PatentProjectDto expand : needPushList) {
            if (expand.getChildStage() == 1) {
                if (workFlowMap.containsKey(expand.getId())) {
                    continue;
                }
                List<String> targetManagerList = new ArrayList<>();
                targetManagerList.add(expand.getProjectManager());
                String content = "风险管控项目【" + expand.getProjectName()+"】 待您发起风险排查申请，请登录处理！";
                MessageConfiguration messageConfiguration = new MessageConfiguration();
                messageConfiguration.setBusinessType(MessagePushBusinessEnum.PATENT_RISK_PROJECT.getValue());
                messageConfiguration.setContent(content);
                messageConfiguration.setContentType(MessageContentTypeEnum.TEXT_CARD.getValue());
                MessagePushRecord mpr = weChatMessageInterface.pushMessage(targetManagerList, null, messageConfiguration, currentTime);
            }
        }
        log.info("checkPatentProjectTask end...");
    }
}

